package Test;

import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.interactions.Actions;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;

public class Before {
	private WebDriver driver;

    @BeforeTest
    public void setup() {

        System.setProperty("webdriver.firefox.bin",
				"C:\\Program Files\\Mozilla Firefox\\firefox.exe");

        driver = new FirefoxDriver();
        driver.get("http://121.40.99.5/Home/user/login.html");
    }

    @Test
    public void testLoginPage() throws InterruptedException {
    	//用户前台登录	
        String username = "13800138006";
        String password = "123456";
        String verify_codeValue = "1234";

        WebElement usernameInput = driver.findElement(By.id("username"));
        WebElement passwordInput = driver.findElement(By.id("password"));
        WebElement verify_codeInput = driver.findElement(By.id("verify_code"));

        usernameInput.sendKeys(username);
        Thread.sleep(500);
        passwordInput.sendKeys(password);
        Thread.sleep(500);
        verify_codeInput.sendKeys(verify_codeValue);
        Thread.sleep(500);

        driver.findElement(By.name("sbtbutton")).click();
        Thread.sleep(1500);
        
    }
    
    public void AddressListPage() throws InterruptedException {
        driver.get("http://www.tpshop.com/Home/User/address_list.html");
        
       	//用户添加地址
        driver.findElement(By.xpath("/html/body/div[3]/div/div[2]/div[2]/div/div[1]/a/span")).click();
        Thread.sleep(1500);
        
        String consignee="王然";
        String mobile="13800138001";
        String address="***";
        String zipcode="123456";
        
        WebElement consigneeInput = driver.findElement(By.name("consignee"));
        WebElement mobileInput = driver.findElement(By.name("mobile"));
        WebElement addressInput= driver.findElement(By.name("address"));
        WebElement zipcodeInput = driver.findElement(By.name("zipcode"));
        
        consigneeInput.sendKeys(consignee);
        Thread.sleep(500);
        mobileInput.sendKeys(mobile);
        Thread.sleep(500);
        driver.findElement(By.id("province")).click();
        Thread.sleep(1500);
        driver.findElement(By.xpath("/html/body/div[4]/form/div[2]/div/div[2]/div[3]/select[1]/option[4]")).click();
        Thread.sleep(1500);
        driver.findElement(By.id("city")).click();
        Thread.sleep(1500);
        driver.findElement(By.xpath("/html/body/div[4]/form/div[2]/div/div[2]/div[3]/select[2]/option[2]")).click();
        Thread.sleep(1500);
        driver.findElement(By.id("district")).click();
        Thread.sleep(1500);
        driver.findElement(By.xpath("/html/body/div[4]/form/div[2]/div/div[2]/div[3]/select[3]/option[2]")).click();
        Thread.sleep(1500);
        addressInput.sendKeys(address);
        Thread.sleep(500);
        zipcodeInput.sendKeys(zipcode);
        Thread.sleep(500);
        driver.findElement(By.id("address_submit")).click();
        Thread.sleep(1500);
        
      //用户设置默认地址
        driver.findElement(By.xpath("/html/body/div[3]/div/div[2]/div[2]/div/div[2]/ul[2]/li[4]/span/a")).click();
        Thread.sleep(1500);
    }
    
    public void OrderListPage() throws InterruptedException{
        driver.get("http://www.tpshop.com/Home/Order/order_list.html");
        
      //用户再次购买商品
        driver.findElement(By.xpath("/html/body/div[3]/div/div[2]/div[2]/div[1]/div[4]/table[2]/tbody/tr[2]/td[6]/div/p/a")).click();
        Thread.sleep(1500);
        driver.findElement(By.xpath("/html/body/div[4]/div/div/div/div[2]/div[2]/div[1]/a")).click();
        Thread.sleep(1500);
        
      //用户查询订单
        String search_key="手机";
        WebElement search_keyInput = driver.findElement(By.id("search_key"));
        search_keyInput.sendKeys(search_key);
        Thread.sleep(500);
        driver.findElement(By.xpath("/html/body/div[3]/div/div[2]/div[2]/div[1]/div[2]/form/input[2]")).click();
        Thread.sleep(1500);
    }
    
    public void InformPage() throws InterruptedException {
    	driver.get("http://www.tpshop.com/Home/User/info.html");
    	
    	//用户修改生日为现在
    	driver.findElement(By.id("birthday")).click();
        Thread.sleep(1500);
        driver.findElement(By.xpath("/html/body/div[5]/div[2]/div/span[2]")).click();
        Thread.sleep(1500);
        
      //用户修改昵称（姓）
        WebElement nicknameToDelete = driver.findElement(By.id("nickname"));
        Actions actions = new Actions(driver);
        actions.moveToElement(nicknameToDelete).click().perform();
        actions.sendKeys(Keys.DELETE).build().perform();
        Thread.sleep(500);
        String nickname="李";
        WebElement nicknameInput = driver.findElement(By.id("nickname"));
        nicknameInput.sendKeys(nickname);
        Thread.sleep(500);
        
      //用户修改性别
        driver.findElement(By.id("woman")).click();
        Thread.sleep(1500);
        
        driver.findElement(By.xpath("/html/body/div[3]/div/div[2]/div[2]/div/div[2]/form/ul[5]/li[2]/div[3]/input")).click();
        Thread.sleep(1500);
    }
    
    public void RechargePage() throws InterruptedException {
    	driver.get("http://www.tpshop.com/Home/User/recharge.html");
    	
    	//用户查询消费记录
    	driver.findElement(By.xpath("/html/body/div[3]/div/div[2]/div[2]/div[3]/div/div/ul/li[4]/a")).click();
        Thread.sleep(1500);
        driver.findElement(By.xpath("/html/body/div[3]/div/div[2]/div[2]/div[3]/div/div/ul/li[4]/a")).click();
        Thread.sleep(1500);
        
      //用户充值
        driver.findElement(By.xpath("/html/body/div[3]/div/div[2]/div[2]/div[1]/div[2]/div[4]/a[2]")).click();
        Thread.sleep(1500);
        
        String input_valValue="123";
        WebElement input_valInput=driver.findElement(By.id("input_val"));
		input_valInput.sendKeys(input_valValue);
        Thread.sleep(1500);
        driver.findElement(By.xpath("/html/body/div[3]/div/div[2]/div[2]/div[2]/div/div[2]/form/div/div[2]/div/div/dl/dd/div/div/ul/li[2]/div/input")).click();
        Thread.sleep(1500);
        driver.findElement(By.xpath("/html/body/div[3]/div/div[2]/div[2]/div[2]/div/div[2]/form/div/div[2]/div/div/div/a[2]")).click();
        Thread.sleep(1500);
        
      //用户提现
        driver.findElement(By.xpath("/html/body/div[3]/div/div[2]/div[2]/div[1]/div[2]/div[4]/a[1]")).click();
        Thread.sleep(1500);
        driver.findElement(By.xpath("/html/body/div[3]/div[1]/div[2]/div[2]/div/div[2]/form/div/dl/dd[2]/label")).click();
        Thread.sleep(1500);
        String moneyValue="123";
        WebElement moneyInput=driver.findElement(By.id("money"));
		moneyInput.sendKeys(moneyValue);
        Thread.sleep(1500);
        String paypwdValue="123456";
        WebElement paypwdInput=driver.findElement(By.id("paypwd"));
        paypwdInput.sendKeys(paypwdValue);
        Thread.sleep(1500);
        driver.findElement(By.id("save_data")).click();
        Thread.sleep(1500);
    }
    
    public void AccountPage() throws InterruptedException {
    	driver.get("http://www.tpshop.com/Home/User/account.html");
    	
    	//用户查看积分明细
    	driver.findElement(By.xpath("/html/body/div[3]/div/div[2]/div[2]/div/form/div[1]/ul/li[1]/a")).click();
        Thread.sleep(1500);
        driver.findElement(By.xpath("/html/body/div[3]/div/div[2]/div[2]/div/div[19]/div/div/ul/li[4]/a")).click();
        Thread.sleep(1500);
        driver.findElement(By.xpath("/html/body/div[3]/div/div[2]/div[2]/div/div[19]/div/div/ul/li[4]/a")).click();
        Thread.sleep(1500);
        
      //用户积分换购
        driver.findElement(By.xpath("/html/body/div[3]/div/div[2]/div[2]/div/div[2]/div[1]/a")).click();
        Thread.sleep(1500);
        driver.findElement(By.xpath("/html/body/div[4]/div/div/div[3]/ul[1]/li[1]/div/div[2]/div/a")).click();
        Thread.sleep(1500);
        driver.findElement(By.id("buy_now")).click();
        Thread.sleep(1500);
    }
    
    public void CommentPage() throws InterruptedException {
    	driver.get("http://www.tpshop.com/Home/Order/comment.html");
    	
    	//用户查看已评论订单
    	driver.findElement(By.xpath("/html/body/div[3]/div/div[2]/div[2]/div[1]/div[2]/div[1]/ul/li[3]/a")).click();
        Thread.sleep(1500);
        driver.findElement(By.xpath("/html/body/div[3]/div/div[2]/div[2]/div[1]/div[2]/div[4]/table[2]/tbody/tr[2]/td[6]/div/p/a")).click();
        Thread.sleep(1500);
        
      //用户查看我的评论
        driver.findElement(By.xpath("/html/body/div[3]/div/div[2]/div[2]/div[1]/div[2]/div[4]/table[1]/tbody/tr[2]/td[6]/div/p/a")).click();
        Thread.sleep(1500);
        driver.findElement(By.xpath("/html/body/div[5]/div/div[2]/div/div[1]/ul/li[3]/a")).click();
        Thread.sleep(1500);
    }
    
    public void ReturnGoodsPage() throws InterruptedException {
    	driver.get("http://www.tpshop.com/Home/Order/return_goods_list.html");
    	
    	//用户查看退换货订单
    	driver.findElement(By.xpath("/html/body/div[3]/div/div[2]/div[2]/div/div[2]/div[3]/div[2]/a")).click();
        Thread.sleep(1500);
    }
    
    public void tpshopPage() throws InterruptedException {
    	driver.get("http://www.tpshop.com/");
    	
    	//用户收藏商品
    	driver.findElement(By.xpath("/html/body/div[5]/div[2]/div[2]/a[3]/div[1]/img")).click();
        Thread.sleep(1500);
        driver.findElement(By.xpath("/html/body/div[3]/div/div[1]/div/div[2]/a/span")).click();
        Thread.sleep(1500);
        
      //用户查找商品
        String q="手机";
        WebElement qInput = driver.findElement(By.id("q"));
        qInput.sendKeys(q);
        Thread.sleep(500);   
        driver.findElement(By.xpath("/html/body/div[1]/div[2]/div[1]/form/button")).click();
        Thread.sleep(1500);
        
      //用户添加商品到购物车
        driver.findElement(By.xpath("/html/body/div[4]/div[2]/div/div[1]/div/div[7]/a/p[1]")).click();
        Thread.sleep(1500);
        driver.findElement(By.id("join_cart")).click();
        Thread.sleep(1500);
        driver.findElement(By.xpath("/html/body/div[14]/span/a")).click();
        Thread.sleep(1500); 
        
      //用户将商城首页回到顶部
        WebElement htmlToEnd = driver.findElement(By.xpath("/html"));
        Actions actions = new Actions(driver);
        actions.moveToElement(htmlToEnd).click().perform();
        actions.sendKeys(Keys.END).build().perform();
        Thread.sleep(500);
        
        driver.findElement(By.xpath("/html/body/div[12]/a[4]/div")).click();
        Thread.sleep(1500);
    }
    
    public void CouponPage() throws InterruptedException {
    	driver.get("http://www.tpshop.com/Home/User/coupon.html");
    	
    	//用户兑换优惠券
    	driver.findElement(By.xpath("/html/body/div[3]/div/div[2]/div[2]/div[1]/div/div/div[1]/span/a")).click();
        Thread.sleep(1500);
        String coupon_codeValue="1234";
        WebElement coupon_codeInput = driver.findElement(By.id("coupon_code"));
        coupon_codeInput.sendKeys(coupon_codeValue);
        Thread.sleep(1500);
        driver.findElement(By.id("coupon_exchange")).click();
        Thread.sleep(1500);
    }
    
    public void GoodsCollectPage() throws InterruptedException {
    	driver.get("http://www.tpshop.com/Home/User/goods_collect.html");
    	
    	//用户删除商品收藏
    	driver.findElement(By.xpath("/html/body/div[3]/div/div[2]/div[2]/div/div[5]/ul[6]/li[1]/input")).click();
        Thread.sleep(1500);
        driver.findElement(By.xpath("/html/body/div[3]/div/div[2]/div[2]/div/div[6]/div[2]/a")).click();
        Thread.sleep(1500);
        driver.findElement(By.xpath("/html/body/div[6]/div[3]/a[1]")).click();
        Thread.sleep(1500);
        
      //用户清空商品收藏
        driver.findElement(By.id("all2")).click();
        Thread.sleep(1500);
        driver.findElement(By.xpath("/html/body/div[3]/div/div[2]/div[2]/div/div[6]/div[2]/a")).click();
        Thread.sleep(1500);
        driver.findElement(By.xpath("/html/body/div[6]/div[3]/a[1]")).click();
        Thread.sleep(1500);
    }
    
    public void CartPage() throws InterruptedException {
    	driver.get("http://www.tpshop.com/Home/Cart/index.html");
    	
    	//用户清空购物车
    	driver.findElement(By.xpath("/html/body/div[3]/div[2]/div/div/div[1]/i")).click();
        Thread.sleep(1500);
        driver.findElement(By.xpath("/html/body/div[4]/div/div/div/div[2]/div[2]/div[1]/a")).click();
        Thread.sleep(1500);
    }
    
    public void MessageNoticePage() throws InterruptedException {
    	driver.get("http://www.tpshop.com/Home/User/message_notice.html");
    	
    	//用户清空消息通知
    	driver.findElement(By.xpath("/html/body/div[3]/div/div[2]/div[2]/div[1]/div[1]/ul/li[2]/a")).click();
        Thread.sleep(1500);
        driver.findElement(By.id("checkAll")).click();
        Thread.sleep(1500);
        driver.findElement(By.xpath("/html/body/div[3]/div/div[2]/div[2]/div[1]/div[2]/div/div[1]/a")).click();
        Thread.sleep(1500);
        driver.findElement(By.xpath("/html/body/div[7]/div[3]/a[1]")).click();
        Thread.sleep(1500);
        
      //用户删除消息
        driver.findElement(By.xpath("/html/body/div[3]/div/div[2]/div[2]/div[1]/div[1]/ul/li[2]/a")).click();
        Thread.sleep(1500);
        driver.findElement(By.xpath("/html/body/div[3]/div/div[2]/div[2]/div[1]/div[2]/div/div[2]/ul/li[2]/div[2]/div[1]/a/img")).click();
        Thread.sleep(1500);
        driver.findElement(By.xpath("/html/body/div[6]/div[3]/a[1]")).click();
        Thread.sleep(1500);
    }
    
    public void LowerListPage() throws InterruptedException {
    	driver.get("http://www.tpshop.com/Home/Order/lower_list.html");
    	
    	//用户查看返利订单
    	driver.findElement(By.xpath("/html/body/div[3]/div/div[2]/div[2]/div[1]/div[2]/div[1]/ul/li[2]/a")).click();
        Thread.sleep(1500);
        driver.findElement(By.xpath("/html/body/div[3]/div/div[2]/div[2]/div[1]/div[2]/div[4]/ul/li[6]/div/p/a")).click();
        Thread.sleep(1500);
    }
    
    public void IndexPage() throws InterruptedException {
    	driver.get("http://www.tpshop.com/Home/user/index.html");
    	
    	//用户VIP充值
    	driver.findElement(By.xpath("/html/body/div[3]/div/div[2]/div[2]/div[1]/div[3]/ul[2]/li[2]/a/span")).click();
        Thread.sleep(1500);
        driver.findElement(By.xpath("/html/body/div[3]/div/div[2]/div[2]/div[1]/div/div[2]/div/div[2]/form/div/div/dl/dd/div/div[2]/ul/li[3]/div/input")).click();
        Thread.sleep(1500);
        driver.findElement(By.xpath("/html/body/div[3]/div/div[2]/div[2]/div[1]/div/div[2]/div/div[2]/form/div/div/div/a")).click();
        Thread.sleep(1500);
        driver.findElement(By.xpath("/html/body/div[6]/div[3]/a[1]")).click();
        Thread.sleep(1500);
    	//用户安全退出
    	driver.findElement(By.xpath("/html/body/div[1]/div/div/div/div[2]/a[2]")).click();
        Thread.sleep(1500);
    }
   @AfterTest
    public void teardown() {
   	if (driver != null) {
        driver.quit();
    	}
    }
}